/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.button;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.ImageIcon;
import javax.swing.text.JTextComponent;

import org.jxSwing.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.button
 * @date 03/03/2014 - 09:01:09
 */
public class ItalicButton extends TextFormatToggleButton {
	
	private static final long serialVersionUID = 6289851690612558230L;
	
	private static ImageIcon ICON_32x32 = null;
	
	private JTextComponent textComponent = null;
	
	private int style;
	
	/**
	 * @param textComponent
	 */
	public ItalicButton(final JTextComponent textComponent) {
	
		super("format-italic", "format-italic", "16x16", new Dimension(25, 25));
		
		ICON_32x32 = getImageIcon("format-italic", "32x32");
		
		this.textComponent = textComponent;
		
		addComponentListener(new ComponentAdapter() {
			
			@Override
			public void componentResized(final ComponentEvent componentEvent) {
			
				//
				if(getSize().getHeight() < 32 || getSize().getWidth() < 32) {
					
					setIcon(getDefaultIcon());
					setSelectedIcon(getDefaultSelectedIcon());
					
				}else {
					
					setIcon(ICON_32x32);
					setSelectedIcon(ICON_32x32);
				}
			}
		});
		
		setStyle(getTextComponent().getFont().getStyle());
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setToolTipText(I18N.font().italic());
		setSelected(getTextComponent().getFont().isItalic());
		
		addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(final ActionEvent actionEvent) {
			
				final Font font = getTextComponent().getFont();
				
				setStyle(font.isPlain() ? Font.PLAIN : font.isBold() ? Font.BOLD : Font.PLAIN);
				
				//
				if( !font.isItalic()) {
					
					setStyle(getStyle() | Font.ITALIC);
				}
				
				getTextComponent().setFont(new Font(font.getName(), getStyle(), font.getSize()));
			}
		});
	}
	
	/**
	 * @param style
	 *        the style to set
	 */
	private void setStyle(final int style) {
	
		this.style = style;
	}
	
	/**
	 * @return the style
	 */
	public final int getStyle() {
	
		return style;
	}
	
	/**
	 * @return the textComponent
	 */
	public final JTextComponent getTextComponent() {
	
		return textComponent;
	}
}
